<?php
/**
 * Created by PhpStorm.
 * User: xiangkevin
 * Date: 30/09/2017
 * Time: 3:57 PM
 */

namespace App\Services;

use JohnLui\AliyunOSS;

use Exception;
use DateTime;

class OssService
{
    /* 城市名称：
    *
    *  经典网络下可选：杭州、上海、青岛、北京、张家口、深圳、香港、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
    *  VPC 网络下可选：杭州、上海、青岛、北京、张家口、深圳、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
    */
    private $city = '北京';

    // 经典网络 or VPC
    private $networkType = '经典网络';

    private $AccessKeyId = 'LTAIn2CwN4i5gR80';
    private $AccessKeySecret = '2Q0lkWyqZc4xOTgiEf40UAZRn0ai0t';


    private $ossClient;

    /**
     * OssService constructor.
     * @param bool $isInternal 是否使用内网
     * @throws Exception 私有初始化 API，非 API，不用关注
     */
    public function __construct($isInternal = false)
    {
        if ($this->networkType == 'VPC' && !$isInternal) {
            throw new Exception("VPC 网络下不提供外网上传、下载等功能");
        }
        $this->ossClient = AliyunOSS::boot(
            $this->city,
            $this->networkType,
            $isInternal,
            $this->AccessKeyId,
            $this->AccessKeySecret
        );
    }



    /**
     * @param $bucketName string bucket名称 qkw-api
     * @param $ossKey     string 上传之后的 OSS object 名称  date   public/uplode/
     * @param $filePath   string 删除文件路径
     * @param array $options
     * @return \Aliyun\OSS\Models\PutObjectResult boolean 上传是否成功
     * @throws Exception
     * @name 使用外网上传文件
     */
    public static function publicUpload($bucketName, $ossKey, $filePath, $options = [])
    {
        $oss = new OssService();
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
    }

    /**
     * @param $bucketName  string bucket名称
     * @param $ossKey      string 上传之后的 OSS object 名称
     * @param $filePath    string 删除文件路径
     * @param array $options
     * @return \Aliyun\OSS\Models\PutObjectResult boolean 上传是否成功
     * @throws Exception
     * @name  使用阿里云内网上传文件·
     */
    public static function privateUpload($bucketName, $ossKey, $filePath, $options = [])
    {
        $oss = new OssService(true);
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
    }

    /**
     * @param $bucketName  string bucket名称
     * @param $ossKey      string 上传之后的 OSS object 名称
     * @param $content     string 删除传的变量
     * @param array $options
     * @return \Aliyun\OSS\Models\PutObjectResult boolean 上传是否成功
     * @throws Exception
     * @name  使用外网直接上传变量内容
     */
    public static function publicUploadContent($bucketName, $ossKey, $content, $options = [])
    {
        $oss = new OssService();
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->uploadContent($ossKey, $content, $options);
    }

    /**
     * @param $bucketName   string bucket名称
     * @param $ossKey       string 上传之后的 OSS object 名称
     * @param $content      string 删除传的变量
     * @param array $options
     * @return \Aliyun\OSS\Models\PutObjectResult boolean 上传是否成功
     * @throws Exception
     * @name 使用阿里云内网直接上传变量内容
     */
    public static function privateUploadContent($bucketName, $ossKey, $content, $options = [])
    {
        $oss = new OssService(true);
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->uploadContent($ossKey, $content, $options);
    }


    /**
     * @param $bucketName   string bucket名称
     * @param $ossKey       string 目标 OSS object 名称
     * @throws Exception    boolean 删除是否成功
     * @name 使用外网删除文件
     */
    public static function publicDeleteObject($bucketName, $ossKey)
    {
        $oss = new OssService();
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->deleteObject($bucketName, $ossKey);
    }

    /**
     * @param $bucketName string bucket名称
     * @param $ossKey     string 目标 OSS object 名称
     * @throws Exception   boolean 删除是否成功
     * @name  使用阿里云内网删除文件
     */
    public static function privateDeleteObject($bucketName, $ossKey)
    {
        $oss = new OssService(true);
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->deleteObject($bucketName, $ossKey);
    }


    /**
     * -------------------------------------------------
     *
     *
     *  下面不再分公网内网出 API，也不注释了，大家自行体会吧。。。
     *
     *
     * -------------------------------------------------
     */

    public function copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
    {
        $oss = new OssService();
        return $oss->ossClient->copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
    }

    public function moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
    {
        $oss = new OssService();
        return $oss->ossClient->moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
    }

    // 获取公开文件的 URL
    public static function getPublicObjectURL($bucketName, $ossKey,$url = "http://img.qikevip.com")
    {
        $oss = new OssService();
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->getPublicUrl($ossKey,$url);
    }

    // 获取私有文件的URL，并设定过期时间，如 \DateTime('+1 day')
    public static function getPrivateObjectURLWithExpireTime($bucketName, $ossKey, DateTime $expire_time)
    {
        $oss = new OssService();
        $oss->ossClient->setBucket($bucketName);
        return $oss->ossClient->getUrl($ossKey, $expire_time);
    }

    public static function createBucket($bucketName)
    {
        $oss = new OssService();
        return $oss->ossClient->createBucket($bucketName);
    }

    public static function getAllObjectKey($bucketName)
    {
        $oss = new OssService();
        return $oss->ossClient->getAllObjectKey($bucketName);
    }

    public static function getObjectMeta($bucketName, $ossKey)
    {
        $oss = new OssService();
        return $oss->ossClient->getObjectMeta($bucketName, $ossKey);
    }
}


